package cn.yeziji.forum.service.impl;

import cn.yeziji.forum.bo.file.FileBO;
import cn.yeziji.forum.common.AliCloudOss;
import cn.yeziji.forum.common.ForumPageResult;
import cn.yeziji.forum.dao.intf.ForumFileDao;
import cn.yeziji.forum.dao.intf.ForumUserDao;
import cn.yeziji.forum.dto.file.ForumFilePagingDTO;
import cn.yeziji.forum.entity.ForumFileEntity;
import cn.yeziji.forum.basic.file.WebUploaderFileEntity;
import cn.yeziji.forum.entity.ForumUserEntity;
import cn.yeziji.forum.service.ProvideService;
import cn.yeziji.forum.utils.AliUtils;
import cn.yeziji.forum.utils.JwtOperaUtils;
import cn.yeziji.forum.utils.entity.AliOssConfig;
import cn.yzj.utils.DataUtils;
import com.alibaba.dubbo.config.annotation.Service;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.io.InputStream;
import java.util.List;

/**
 * 论坛文件服务逻辑实现层
 *
 * @author gzkemays
 * @since 2022/5/12 17:00
 */
@Service
@Component
public class ProvideServiceImpl implements ProvideService {
  @Resource ForumFileDao fileDao;
  @Resource AliCloudOss oss;
  @Resource ForumUserDao userDao;

  @Override
  public boolean checkToken(String token) {
    return JwtOperaUtils.checkToken(token);
  }

  @Override
  public Integer getProcess(String md5) {
    ForumFileEntity entity = fileDao.getEntityByTarget(md5);
    if (entity == null) {
      return 0;
    }
    String scope = entity.getScope();
    if (StringUtils.isNotBlank(scope) && scope.lastIndexOf("*") != -1) {
      String chunks = DataUtils.substrAfterOfLastTag(scope, "*");
      try {
        return Integer.parseInt(chunks);
      } catch (Exception e) {
        return 0;
      }
    }
    return 0;
  }

  @Override
  public void save(WebUploaderFileEntity wfe) {
    fileDao.save(new FileBO(wfe));
  }

  @Override
  public String getUsernameByToken(String token) {
    if (checkToken(token)) {
      return JwtOperaUtils.getUserNameByToken(token);
    }
    return null;
  }

  @Override
  public ForumUserEntity getUserEntityByToken(String token) {
    if (checkToken(token)) {
      Integer idByToken = JwtOperaUtils.getIdByToken(token);
      return userDao.selectById(idByToken);
    }
    return null;
  }

  @Override
  public List<ForumFileEntity> getFileEntitiesBySource(String source) {
    return fileDao.getEntitiesBySource(source);
  }

  @Override
  public List<ForumFileEntity> getFileEntitiesByScope(String scope) {
    return fileDao.getEntitiesByScope(scope);
  }

  @Override
  public ForumPageResult<ForumFileEntity> getFileEntitiesPage(ForumFilePagingDTO pagingDTO) {
    return fileDao.getFileEntitiesPage(pagingDTO);
  }

  @Override
  public String uploadToOss(String savePath, String filename, InputStream stream) {
    return AliUtils.uploadToOss(
        AliOssConfig.cloudOss(oss).savePath(savePath).fileName(filename).stream(stream).build());
  }

  @Override
  public void del(String scope, String source) {
    fileDao.del(new FileBO(scope, source));
  }

  public static void main(String[] args) {
    System.out.println(JwtOperaUtils.checkToken("123"));
  }
}
